package com.zja.dao.Impl;

import com.zja.dao.BaseDAO;
import com.zja.dao.UserDao;
import com.zja.model.User;

import java.util.List;

/**
 * @author JA
 */
public class UserDaoImpl extends BaseDAO implements UserDao {

    @Override
    public List<User> selectAll() {
        String sql = "select * from user";
        try {
            return executeQuery(User.class,sql,null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public User selectById(Long id) {
        try {
            String sql = "select * from user where id = ?";
            return executeQueryBean(User.class,sql,id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }


    @Override
    public boolean insert(User user) {
        try {
            String sql = "INSERT INTO user(username, password, email, avatar, create_time, update_time, role) VALUES(?,?,?,?,?,?,?)";
            return executeUpdate(sql,user.getUsername(),user.getPassword(),user.getEmail(),user.getAvatar(),user.getCreateTime(),user.getUpdateTime(),user.getRole());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean update(User user) {
        try {
            String sql = "UPDATE user SET username = ?, password = ?, email = ?, avatar = ?, update_time = ?, role = ? WHERE id = ?";
            return executeUpdate(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getAvatar(), user.getUpdateTime(), user.getRole(), user.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean deleteById(Long id) {
        try {
            String sql = "DELETE FROM user WHERE id = ?";
            return executeUpdate(sql,id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public User selectByUserName(String username) {
        try {
            String sql = "select * from user where username = ?";
            return executeQueryBean(User.class,sql,username);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public User selectByEmail(String email) {
        try {
            String sql = "select * from user where email = ?";
            return executeQueryBean(User.class,sql,email);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }

}
